﻿/* 
 * yaku
 * http://code.google.com/p/yaku/
 * 
 * Copyright 2011, Bryan McKelvey
 * Licensed under the MIT license
 * http://www.opensource.org/licenses/mit-license.php
 */

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows;

namespace yaku
{
    /// <summary>
    /// A class for localizing an application using XAML resource dictionaries.
    /// </summary>
    public class Localization
    {
        private const string resPath = "Localization";
        private const string resPrefix = "Localization.";
        private const string resLangPattern = "[a-z]{2}";
        private const string resSuffix = ".xaml";
        private const string defaultCulture = "en";

        public static void SetCulture()
        {
            SetCulture(SavedName, GetDictionary(SavedName));
        }

        public static void SetCulture(string culture)
        {
            SetCulture(culture, GetDictionary(culture));
        }

        public static void SetCulture(string culture, ResourceDictionary dictionary)
        {
            if (dictionary == null)
            {
                dictionary = GetDictionary(defaultCulture);
            }
            if (dictionary != null)
            {
                AppDictionaries.Remove(dictionary);
                AppDictionaries.Add(dictionary);
            }
            Culture = new CultureInfo(culture);
            SavedName = culture;
        }

        #region Private methods
        private static ResourceDictionary GetDictionary(string culture)
        {
            string originalString = GetPath(culture);
            return AppDictionaries.FirstOrDefault(d => d.Source.OriginalString == originalString);
        }

        private static string GetPath(string culture)
        {
            return string.Format(Template, culture);
        }
        #endregion

        #region Properties
        private static Collection<ResourceDictionary> AppDictionaries
        {
            get { return Application.Current.Resources.MergedDictionaries; }
        }

        public static string Pattern
        {
            get { return GetPath(resLangPattern); }
        }

        public static string Template
        {
            get { return (resPath == "" ? "" : resPath.TrimEnd('/') + "/") + resPrefix + "{0}" + resSuffix; }
        }

        /// <summary>
        /// Gets or sets the current culture used by the Resource Manager to look up culture-specific resources at run time.
        /// </summary>
        public static CultureInfo Culture
        {
            get { return Thread.CurrentThread.CurrentUICulture; }
            set { Thread.CurrentThread.CurrentUICulture = value; }
        }

        /// <summary>
        /// Gets the ISO 639-1 two-letter code for the language of the current culture. 
        /// </summary>
        public static string Name
        {
            get { return Culture.TwoLetterISOLanguageName; }
            set { SetCulture(value); }
        }

        public static string SavedName
        {
            get { return Properties.Settings.Default.CultureName; }
            set { Properties.Settings.Default.CultureName = value; }
        }
        #endregion
    }
}